package core.services;

import com.hexiong.jdbf.DBFReader;
import com.hexiong.jdbf.JDBFException;
import core.models.DBFModel1;
import core.models.DBFModel2;
import core.models.DbfData;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 28.04.2014.
 */
public class ReadDbfService {

    public static void main(String[] args) {
        //  testRead();
        List<DbfData> results = null;
        try {
            results = getDbfFileContent("D:\\book2.dbf");
            System.out.println("readed: " + results.size());
        } catch (JDBFException e) {
            e.printStackTrace();
        }
    }

    public static List<DbfData> getDbfFileContent(String path) throws JDBFException {
        List<DbfData> results = new ArrayList<DbfData>();
            DBFReader reader = new DBFReader(path);
            while (reader.hasNextRecord()) {
                if (reader.getFieldCount() == 27) {
                    results.add(readFromModel1(reader.nextRecord()));
                } else if (reader.getFieldCount() == 12) {
                    results.add(readFromModel2(reader.nextRecord()));
                }
            }

        return results;
    }

    public static DBFModel1 readFromModel1(Object[] row) {
        DBFModel1 result = new DBFModel1();
        result.availableYear = getRowData(row[0]);
        result.drivingLicenseNo = getRowData(row[1]);
        result.countryName = getRowData(row[2]);
        result.drivingLicenseType = getRowData(row[3]);
        result.registrationNumber = getRowData(row[4]);
        result.countryCode = getRowData(row[5]);
        result.codeType = getRowData(row[6]);
        result.note = getRowData(row[7]);
        result.pasagerCode = getRowData(row[8]);
        result.pasagerName = getRowData(row[9]);
        result.fiscalCode = getRowData(row[10]);
        result.sellerCode = getRowData(row[11]);
        result.sellDate = getRowData(row[12]);
        result.amount = getRowData(row[13]);
        result.returned = getRowData(row[14]);
        result.valabilityDate = getRowData(row[15]);
        result.returnedDate = getRowData(row[16]);
        result.economicAgentName = getRowData(row[17]);
        result.address = getRowData(row[18]);
        result.marcaMTA = getRowData(row[19]);
        result.modelMTA = getRowData(row[20]);
        result.euroMTA = getRowData(row[21]);
        result.certificate = getRowData(row[22]);
        result.certificateDate = getRowData(row[23]);
        result.nextTestDate = getRowData(row[24]);
        result.codeVin = getRowData(row[25]);
        result.engineCode = getRowData(row[26]);

        return result;
    }

    public static DBFModel2 readFromModel2(Object[] row) {
        DBFModel2 result = new DBFModel2();
        result.availableYear = getRowData(row[0]);
        result.availableYear = getRowData(row[0]);
        result.countryCode = getRowData(row[0]);
        result.countryName = getRowData(row[0]);
        result.type = getRowData(row[0]);
        result.nvid = getRowData(row[0]);
        result.restriction = getRowData(row[0]);
        result.valability = getRowData(row[0]);
        result.start = getRowData(row[0]);
        result.end = getRowData(row[0]);
        result.date = getRowData(row[0]);
        result.corrKinsp = getRowData(row[0]);
        result.corrDate = getRowData(row[0]);

        return result;
    }

    private static String getRowData(Object data) {
        return data != null ? data.toString() : "";
    }
}
